package com.tencent.welink.config;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class BussinessConfig {    
    private static final Logger log = LogManager.getLogger(BussinessConfig.class);

    public static String appId;
    public static String appKey;
    public static String appTicket;
    public static String httpProtocal;
    public static String httpDomain;
   // public static String httpPreFix;
    public static String loginByapp="/common/ticket/loginByApp";
    public static int loginDurationMinutes=18;
    public static int encyrptingbyteslen=128;

    public  static String ip ;
    public  static int port = 18831;

    public static String pid ;
    public static String sn ;
    public static String PRODUCT_PRIVATE_KEY;
    public static String SERVER_PUBLIC_KEY;
    public static String websocket;
    public static List<String> topiclist = new ArrayList<>();
    public static String weilinkConfigFile;
    public static int mqttDurationHours;
    public static String videoFileName = "default";
    public BussinessConfig(){
        init();
    }
    
    public void init(){
        try {
            load();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            log.error("load config error,exit!",e);
            System.exit(1);
        }
    }

    private void loadApplication() throws IOException {
        InputStream resStream = BussinessConfig.class
                .getResourceAsStream("/config.properties");

        Properties props = new Properties();
        props.load(resStream);

        weilinkConfigFile = StringUtils.strip(props.getProperty("weilinkConfigFile"));

        try {
            if(resStream!= null)
                resStream.close();
        }catch (Exception e){
            log.error(e);
        }
    }

    private void load() throws IOException{
        loadApplication();
        String fileName = "welink-java-sdk.properties";
        if(StringUtils.isNotBlank(weilinkConfigFile)) {
            fileName = "welink-java-sdk-"+weilinkConfigFile+".properties";
        }

        InputStream resStream = BussinessConfig.class
                .getResourceAsStream("/"+fileName);

        Properties props = new Properties();
        props.load(resStream);
        log.info("get config" );
        appId = StringUtils.strip(props.getProperty("appId"));
        appKey = StringUtils.strip(props.getProperty("appKey"));
        appTicket = StringUtils.strip(props.getProperty("appTicket"));
        httpProtocal = StringUtils.strip(props.getProperty("httpProtocal"));
        httpDomain = StringUtils.strip(props.getProperty("httpDomain"));

       // httpPreFix = StringUtils.strip(props.getProperty("httpPreFix"));
        //loginByapp=/common/ticket/loginByApp
        loginByapp = StringUtils.strip(props.getProperty("loginByapp"));
        //loginDurationMinutes=18
        int loginDurationMinutesTemp = 18;
        try {
            loginDurationMinutesTemp = Integer.valueOf(StringUtils.strip(props.getProperty("loginDurationMinutes")));
            if(loginDurationMinutesTemp<=0) loginDurationMinutesTemp = 18;
            if(loginDurationMinutesTemp>20) loginDurationMinutesTemp = 18;
        }catch (Exception e) {
            loginDurationMinutesTemp = 18;
        }
        loginDurationMinutes = loginDurationMinutesTemp * 60 * 1000;


        int mqttDurationHoursTemp = 23;
        try {
            mqttDurationHoursTemp = Integer.valueOf(StringUtils.strip(props.getProperty("mqttDurationHours")));
            if(mqttDurationHoursTemp<=0) mqttDurationHoursTemp = 23;
            if(mqttDurationHoursTemp>=24) mqttDurationHoursTemp = 23;
        }catch (Exception e) {
            mqttDurationHoursTemp = 23;
        }
        mqttDurationHours = mqttDurationHoursTemp * 60 * 60 * 1000;

        try {
            BussinessConfig.encyrptingbyteslen = Integer.valueOf(StringUtils.strip(props.getProperty("encyrptingbyteslen")));
            if(BussinessConfig.encyrptingbyteslen<64){
                BussinessConfig.encyrptingbyteslen = 64;
            }
            if(BussinessConfig.encyrptingbyteslen>2048){
                BussinessConfig.encyrptingbyteslen = 2048;
            }
        } catch (Exception e) {
            BussinessConfig.encyrptingbyteslen = 128;

        }

        BussinessConfig.ip = StringUtils.strip(props.getProperty("ip"));

        try {
            BussinessConfig.port = Integer.valueOf(StringUtils.strip(props.getProperty("port")));

            if(BussinessConfig.port<=0){
                BussinessConfig.port = 18831;
            }
        } catch (Exception e) {
            BussinessConfig.port = 18831;

        }

        BussinessConfig.pid = StringUtils.strip(props.getProperty("pid"));
        BussinessConfig.sn = StringUtils.strip(props.getProperty("sn"));

        BussinessConfig.SERVER_PUBLIC_KEY = StringUtils.strip(props.getProperty("SERVER_PUBLIC_KEY"));
        BussinessConfig.PRODUCT_PRIVATE_KEY = StringUtils.strip(props.getProperty("PRODUCT_PRIVATE_KEY"));
        BussinessConfig.websocket = StringUtils.strip(props.getProperty("websocket"));
        BussinessConfig.videoFileName = StringUtils.strip(props.getProperty("videoFileName"));
        if(StringUtils.isBlank(BussinessConfig.videoFileName)){
            BussinessConfig.videoFileName = "default";
        }
        try {
            BussinessConfig.topiclist.addAll(JSONObject.parseArray(props.getProperty("topiclist"), String.class));
        }catch (Exception e) {
            log.error(e);
        }
        log.info("appId: " + appId);
        log.info("appKey: " + appKey);
        log.info("appTicket: " + appTicket);
        log.info("httpProtocal: " + httpProtocal);
        log.info("httpDomain: " + httpDomain);

       // log.info("httpPreFix: " + httpPreFix);
        log.info("loginByapp: " + loginByapp);
        log.info("loginDurationMinutes: " + loginDurationMinutes);
        log.info("encyrptingbyteslen: " + encyrptingbyteslen);
        log.info("ip: " + ip);
        log.info("port: " + port);
        log.info("pid: " + pid);
        log.info("sn: " + sn);
        log.info("SERVER_PUBLIC_KEY: " + SERVER_PUBLIC_KEY);
        log.info("PRODUCT_PRIVATE_KEY: " + PRODUCT_PRIVATE_KEY);
        log.info("websocket: " + websocket);
        log.info("topiclist: " + topiclist);
        log.info("weilinkConfigFile: " + weilinkConfigFile);
        log.info("fileName: " + fileName);
        log.info("mqttDurationHours: " + mqttDurationHours);
        log.info("videoFileName: " + BussinessConfig.videoFileName);
        try {
            if(resStream!= null)
                resStream.close();
        }catch (Exception e){
            log.error(e);
        }
    }

}
